SNFMessage = { }  -- this is the public global object, used as a namespace

local msgq = { } -- this is the private local object, used for data storage
		 -- it contains outbound messages to be sent to addon channel as SMSYNC
local msgcount = 0

local tellq = { }

local tellcount = 0

function SNFMessage:Queue(msg)
	table.insert(msgq, msg)
end

function SNFMessage:CheckSync()  -- called every once in 100 updates or so

	if not msgq then msgq = { } end
	if #(msgq) == 0 then
		SNFSmartSync:CheckToDoQueue()
	end
	if #(msgq) > 0 then

		local msg = table.remove(msgq, 1)
		if msg then
			SNFDEBUG(3, "-> "..gsub(msg, "\124", "\124\124"));
			SendAddonMessage(CHAT_PREFIX_SMSYNC, msg, "RAID", nil)
			msgcount = msgcount + 1
			if #(msgq) == 0 then
				SNFDEBUG(2, "Number of messages sent this session: "..msgcount)
			end
		else
			SNFDEBUG(4, "Queue size is "..#(msgq)..".  Nothing being removed")
		end
	else
		--SNFDEBUG(3, "AutoSyncMessage: Nothing to send.");
			SNFMessage:CheckSyncTell()
	end
end

function SNFMessage:QueueTell(msg, who)
	local q = { }
	q.msg = msg
	q.who = who
	table.insert(tellq, q)

end

function SNFMessage:CheckSyncTell()  -- called every once in 100 updates or so

	if not tellq then tellq = { } end
	if #(tellq) > 0 then

		local tell = table.remove(tellq, 1)
		if tell and tell["msg"] and tell["who"] then
			SNFDEBUG(4, tell["who"].."-> "..gsub(tell["msg"], "\124", "\124\124"));
			SendChatMessage(tell["msg"], "WHISPER", nil, tell["who"])
			tellcount = tellcount + 1
			if #(tellq) == 0 then
				SNFDEBUG(2, "Number of messages sent this session: "..tellcount)
			end
		else
			SNFDEBUG(4, "Queue size is "..#(tellq)..".  Nothing being removed")
		end
	else
		--SNFDEBUG(3, "AutoSyncMessage: Nothing to send.");
	end
end